<?php
/**
* 介绍模型
* @DateTime 2017-09-04
* @author xyl
*/
class Introduce_model extends MY_Model
{
	private $table = 'introduce';
	private $_typename = array(
		'1'=>'临平新城',
		'2'=>'艺尚小镇',
		'3'=>'东湖绿道',
		'4'=>'绿道设施',
		);
	function __construct()
	{
		parent::__construct();

	}

	

	/**
	 * 获取单条资讯详情
	 * @DateTime 2017-09-07
 	 * @author xyl
	 * @param  [type] $where   [查询条件]
	 * @return [type]          [description]
	 */
	public function getIntroduceDetail($type)
	{
		$where['type'] = $type;
		$info = $this->hw_getRecord($this->table,$where);
		$data['count'] = 2;
		if (!empty($info)) { 
			//如果存在信息
			$data['info'] = $info;
			if (!empty($info['relation_id'])) {
				//将字符串转化成数组
				$relation = explode(',', $info['relation_id']);
				$relation_info = array();
				//如果需要资讯详情，循环获取资讯详情fo)) {
				for ($i=0; $i < count($relation); $i++) { 
					$relation_info[] = $this->hw_getRecord('information',array('id'=>$relation[$i]));
				}
				$data['relation'] = $relation_info;
				$data['count'] = 2-count($relation);
			}
		}
		$data['type_name'] = $this->_typename[$type];
		return $data;
	}

	/**
	 * 添加或更新介绍信息
	 * @DateTime 2017-09-07
 	 * @author xyl
	 * @param [type] $post [description]
	 */
	public function addIntroduce($post,$type)
	{
		$data = array(
			'relation_id'	=>	isset($post['news'])?implode(',', $post['news']):'',
			'content' 		=>	$post['details'],
			'theme'			=>	$post['theme'],
			'default_img' 	=>	$post['default_img'],
			);
		$return = $this->getIntroduceDetail($type);
		$type_name = $this->_typename[$type];
		if (!empty($return['info'])) {
			//如果有这个类型的数据，编辑
			$info = $return['info'];
			$data['update_time'] = date('Y-m-d H:i:s');
			$where['type'] = $type;
			$log_details = array('before'=>$info,'after'=>$data);
			$res = $this->hw_Update($this->table,$data,$where);
			$msg = '编辑';
			$type = 2;
			$id = $info['id'];
		} else {
			//如果没有则新增
			$data['type'] = $type;
			$res = $this->hw_Insert($this->table,$data);
			$msg = '添加';
			$type = 1;
			$log_details = $data;
			$id = $res;
		}
		if ($res) {
			$result = array('code'=>1, 'msg'=>$msg.$type_name.'信息成功');
		} else {
			$result = array('code'=>0, 'msg'=>$msg.$type_name.'信息失败');
		}
		//添加操作日志
		$this->logMdl->addOperationLog($result['code'],$type,$id,$this->table,$type_name,json_encode($log_details));
		return $result;
	}

	public function appGetIntroduce($type)
	{
		
	}
	
}